﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using myMvcBlog.Infrastructure;
using myMvcBlog.Models;

namespace myMvcBlog.Controllers
{
    public class PostController : BaseController
    {
        //
        // GET: /Post/

        [UserInfoParse]
        public new ActionResult View(string niceName)
        {
            Post post = Post.SingleOrDefault(p => p.UrlName == niceName);
            if (post == null)
            {
                return RedirectToAction("NotFound");
            }

            IList<Tag> tags = (from pt in post.PostTags
                               join t in Tag.All() on pt.TagID equals t.ID
                               select t).ToList();

            IList<Category> categories = (from pc in post.PostCategories
                                          join c in Category.All() on pc.CategoryID equals c.ID
                                          select c).ToList();

            PostView pv = new PostView()
            {
                Post = post,
                Categories = categories,
                Tags = tags,
                Comments = post.Comments.ToList(),
                AddComment = new AddCommentView()
                {
                    PostId = post.ID,
                    User = RouteData.Values["User"] + string.Empty,
                    Email = RouteData.Values["Email"] + string.Empty,
                    ImageSrc = RouteData.Values["ImageSrc"] + string.Empty,
                    PostUrl = niceName,
                    IsKnownUser = !string.IsNullOrEmpty(RouteData.Values["Email"] + string.Empty)
                }
            };

            return View(pv);
        }
    }
}
